package com.qianxp.auth_datajpa.controller;

import com.qianxp.auth_datajpa.dao.AreaDao;
import com.qianxp.auth_datajpa.domain.Area;
import com.qianxp.auth_datajpa.entity.AjaxJson;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Controller
@RequestMapping("area")
@ResponseBody
public class AreaController {
    private AreaDao areaDao;
    @RequestMapping(value = "save",method = RequestMethod.GET)
    public AjaxJson insert(Area area){
        if(Objects.isNull(area)){
            return new AjaxJson().Fail().setMessage(AjaxJson.emptyParamsMsg);
        }
        try {
            Area areaR = areaDao.save(area);
            return new AjaxJson().Success().setData(areaR);
        }catch (Exception e){
            return new AjaxJson().Fail();
        }
    }
    @RequestMapping(value = "find",method = RequestMethod.GET)
    public AjaxJson listArea(Area area){
        Example<Area> areaExample = Example.of(area);
        Optional<Area> optional = areaDao.findOne(areaExample);
        if(optional.isPresent()){
            return new AjaxJson().Success().setData(optional.get());
        }
        return new AjaxJson().Fail();
    }




    @Autowired
    public void setAreaDao(AreaDao areaDao) {
        this.areaDao = areaDao;
    }
}
